<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' width="67%">
        <div class="search">

            <Row class="operation">
                <Select v-model="searchForm.cataLogId" style="width:200px;padding-right: 5px;" placeholder="请选择一级目录" @on-change="cataLogChange">
                    <Option value="0" key="0" label="一级目录"></Option>
                    <Option v-for="item in cataLogList" :value="item.id" :key="item.id" :label="item.cataLogTitle"></Option>
                </Select>
                <Button @click="delAll" icon="md-trash">批量删除</Button>
                <Button @click="getDataList" icon="md-refresh">刷新</Button>
                <circleLoading v-if="operationLoading"/>
            </Row>
            <Row>
                <Alert show-icon>
                    已选择 <span class="select-count">{{selectCount}}</span> 项
                    <a class="select-clear" @click="clearSelectAll">清空</a>
                </Alert>
            </Row>
            <Row>
                <Table :loading="loading" border :columns="columns"  size="small" :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
            </Row>


        </div>
    </Modal>
</template>

<script>
    import circleLoading from "../../my-components/circle-loading.vue";
    import {cataLogList, delCataLog, updateCataLogNo, selectFirstTreeData, updateCataLogSortOrder,updateSortOrder} from "@/api/bus";

    export default {
        name: "catlog-Modal",
        components: {
            circleLoading
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                drop: false,
                modalTitle: '课程目录',
                modalVisible: false,
                selectCount: 0, // 多选计数
                cataLogList: [],
                courseId: 0,
                selectList: [], // 多选数据
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                searchForm: {
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    courseTitle: "",
                    parentId: 0,
                    id: 0,
                    cataLogId: 0,
                    userName: "",
                    startDate: "",
                    endDate: ""
                },
                selectDate: null, //选择日期绑定modal
                columns: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "编号",
                        key: "cataLogNo",
                        width: 100,
                        sortable: true,
                        render: (h, params) => {
                            return h("Input", {
                                props: {
                                    value: params.row.cataLogNo,
                                },
                                on: {
                                    "on-change": v => {
                                        this.updateCataLogNo(params.row.id, v.target.value);
                                    }
                                }
                            });
                        }
                    },{
                        title: "排序号",
                        key: "sortOrder",
                        width: 100,
                        sortable: true,
                        render: (h, params) => {
                            return h("InputNumber", {
                                props: {
                                    value: params.row.sortOrder,
                                    min:0
                                },
                                on: {
                                    "on-change": v => {
                                        this.updateSortOrder(params.row.id, v);
                                    }
                                }
                            });
                        }
                    },
                    {
                        title: "标题名称",
                        key: "cataLogTitle",
                        ellipsis: true,
                        tooltip: true,
                        width: 250,
                        sortable: true
                    },
                    {
                        title: "阿里云视频ID",
                        key: "videoId",
                        width: 150,
                        sortable: true,
                        render: (h, params) => {
                            if (params.row.videoId === '') {
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "#19be6b"
                                            }
                                        },
                                        "未上传"
                                    )
                                ]);
                            }else{
                                return h("div", [
                                    h(
                                        "Tag",
                                        {
                                            props: {
                                                color: "#ff9900"
                                            }
                                        },
                                        "已上传"
                                    )
                                ]);
                            }
                        }
                    },

                    {
                        title: "播放时长",
                        key: "playTime",
                        width: 120,
                        sortable: true
                    },

                    {
                        title: "是否免费",
                        key: "isFree",
                        width: 120,
                        sortable: true
                    },
                    {
                        title: "创建时间",
                        key: "createTime",
                        width: 200,
                        sortable: true
                    }, {
                        title: "操作",
                        key: "action",
                        width: 120,
                        align: "center",
                        fixed: "right",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "error",
                                            size: "small"
                                        },
                                        on: {
                                            click: () => {
                                                this.remove(params.row);
                                            }
                                        }
                                    },
                                    "删除"
                                )
                            ]);
                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {

            },
            getDataList() {
                this.loading = true;
                cataLogList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });
            },
            //更新课程目录
            updateCataLogNo(id, cataLogNo) {
                let params = {
                    id, cataLogNo
                }
                updateCataLogNo(params).then(res => {
                    if (res.status === true) {
                        //this.$Message.success(res.info);
                    }
                });
            },
            //更新排序号
            updateSortOrder(id, sortOrder) {
                let params = {
                    id, sortOrder
                }
                updateSortOrder(params).then(res => {
                    if (res.status === true) {
                        //this.$Message.success(res.info);
                    }
                });
            },
            cataLogChange(v) {
                this.searchForm.parentId = v;
                this.getDataList();
            },
            //加载一级课程目录
            getFirstCataLog() {
                this.treeLoading = true;
                selectFirstTreeData({courseId: this.courseId}).then(res => {
                    if (res.status === true) {
                        this.cataLogList = res.data;
                    }
                });
            },
            onDragDrop(index1, index2) {
                var params = {
                    courseId: this.courseId,
                    parentId: this.searchForm.parentId,
                    n1: index1,
                    n2: index2
                }
                updateCataLogSortOrder(params).then(res => {
                    if (res.status === true) {
                        this.$Message.success(res.info);
                        this.getDataList();
                    }
                });
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除?",
                    onOk: () => {
                        delCataLog(v.id).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.getDataList();
                            }
                        });
                    }

                });
            },
            open(courseId) {
                this.searchForm.id = courseId;
                this.courseId = courseId;
                this.getDataList();
                this.getFirstCataLog();
                this.modalVisible = true;
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                // 重新加载数据
                this.getDataList();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            delAll() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要删除的数据");
                    return;
                }
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                    onOk: () => {
                        let ids = [];
                        this.selectList.forEach(function (e) {
                            ids.push(e.id);
                        });
                        delCataLog(ids).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.clearSelectAll();
                                this.getDataList();
                            }
                        });
                    }
                });
            }
        },
        mounted() {
            this.init();
        }
    }
</script>
